package io.github.lee.app.unsplash.ui.main.home.page

import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import com.chad.library.adapter.base.util.setOnDebouncedItemClick
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.app.unsplash.repository.domain.Topic
import io.github.lee.app.unsplash.ui.detail.photo.PhotoDetailActivity
import io.github.lee.core.ui.BaseRefreshFragment
import io.github.lee.core.util.getArgumentParcelable

@AndroidEntryPoint
class TopicPhotoListFragment : BaseRefreshFragment<TopicPhotoListVM>() {
    companion object {
        private const val KEY_TOPIC = "TOPIC"
        fun newInstance(topic: Topic): TopicPhotoListFragment {
            val args = bundleOf(
                KEY_TOPIC to topic
            )
            val fragment = TopicPhotoListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateViewModel(): TopicPhotoListVM =
        viewModels<TopicPhotoListVM>().value.also {
            val topic = getArgumentParcelable(KEY_TOPIC, Topic::class.java)
            if (null == topic) {
                showError()
                return@also
            }
            it.topic = topic
        }

    //===Desc:===============================================================================

    override fun onSetViewListener() {
        super.onSetViewListener()
        vm?.adapter?.setOnDebouncedItemClick { adapter, _, position ->
//            TestActivity.start(mContext)
            PhotoDetailActivity.start(mContext, adapter.items[position])
        }
    }

    override fun onSetViewData() {
        super.onSetViewData()
        setupRefreshLayout {
            it.setProgressViewOffset(true, 300, 400)
        }
        val headerAdapter = TopicPhotoHeaderAdapter(vm?.topic)
        addHeaderAdapter(headerAdapter)
    }


}